package org.scboy.rbac.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.rbac.dao.RbacLoginSessionDao;
import org.scboy.rbac.dao.RbacLoginStateDao;
import org.scboy.rbac.dao.RbacResourceSysDao;
import org.scboy.rbac.dao.RbacSystemDao;
import org.scboy.rbac.dao.RbacUserInfoDao;
import org.scboy.rbac.service.SystemService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.scboy.utils.enums.coobi.OrderType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.lang.UUID;


/**
 * 
 * 系统业务类
 * 
 * @author yanghao
 *
 */

@Service
public class SystemServiceImp implements SystemService {

	@Autowired
	EntityFactory factory;
	
	/**
	 * 分页查询系统
	 * 
	 * @param model
	 *            查询参数
	 * @param limit
	 *            分页参数
	 * @return
	 */
	@Override
	public Page page(Model model) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		model.orderBy("sysSort");
		return dao.page(model);
	}

	/**
	 * 新增系统
	 * 
	 * @param model
	 *            系统Model
	 * @return
	 */
	@Override
	public boolean add(Model model) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		model.add("id", UUID.randomUUID().toString());
		
		int sysSort=dao.max(new String[] {"sysSort"}, Model.create()).getInt("sysSort");
		
		model.add("sysState", 0);
		model.add("sysDisplay", 1);
		model.add("sysSort", sysSort+1);
		model.add("addTime", new Date());
		model.add("editTime", new Date());
		int result = dao.insert(model);
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 删除系统
	 * 
	 * @param id
	 *            系统ID
	 * @return
	 */
	@Override
	public boolean delete(String id) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		RbacResourceSysDao resourceSysDao=factory.getDao(RbacResourceSysDao.class);
		// 删除菜单角色关联
		resourceSysDao.delete(
				Model.create().add("sysId", id));
		int result = dao.delete(
				Model.create().add("id", id));
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 更新系统
	 * 
	 * @param model
	 *            系统Model
	 * @return
	 */
	@Override
	public boolean update(Model model) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		model.add("editTime", new Date());
		int result = dao.update(model,
				Model.create().add("id", model.getString("id")));
		if (result > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 启用禁用
	 * 
	 * @param id
	 *            系统ID
	 * @return
	 */
	@Override
	public boolean checkState(String id) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		Model model = get(id);
		if (model == null) {
			return false;
		}
		model.add("sysState", !model.getBoolean("sysState"));
		int resultCount = dao.update(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 根据系统ID获取系统信息
	 * 
	 * @param id
	 *            系统ID
	 * @return
	 */
	@Override
	public Model get(String id) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		return dao.get(Model.create().add("id", id));
	}

	@Override
	public boolean display(String id) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		Model model = get(id);
		if (model == null) {
			return false;
		}
		
		model.add("sysDisplay", !model.getBoolean("sysDisplay"));
		int resultCount = dao.update(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	@Transactional
	public boolean sort(String id, int type) {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		Model sysModel=dao.get(Model.create().add("id", id));
		Model lastSys=null;
		//降序
		if(type==0) {
			//查询下面一个系统
			lastSys=dao.get(Model.create().GT("sysSort", sysModel.getInt("sysSort")).orderBy("sysSort"));
			
		}else {
			//升序
			lastSys=dao.get(Model.create().LT("sysSort", sysModel.getInt("sysSort")).orderBy("sysSort",OrderType.DESC));
			
		}
		if(lastSys!=null) {
			dao.update(Model.create().add("sysSort", lastSys.getInt("sysSort")),Model.create().add("id", id));
			dao.update(Model.create().add("sysSort", sysModel.getInt("sysSort")),Model.create().add("id", lastSys.getString("id")));
		}
		return true;
	}

	@Override
	public List<Model> list(String token) {
		RbacLoginSessionDao sessionDao=factory.getDao(RbacLoginSessionDao.class);
		RbacLoginStateDao dao=factory.getDao(RbacLoginStateDao.class);
		RbacSystemDao systemDao=factory.getDao(RbacSystemDao.class);
		RbacUserInfoDao userInfoDao=factory.getDao(RbacUserInfoDao.class);
		
		Model loginState=dao.get(Model.create().add("sessionId", token).add("sessionState", 0));
		if(loginState!=null) {
			String userId=loginState.getString("userId");
			
			//检查用户是不是超级管理员
			long count=userInfoDao.count(Model.create().add("id", userId).add("userType", "administrator"));
			if(count>0) {
				//获取所有配置的系统
				List<Model> systems=systemDao.select(Model.create().orderBy("sysSort"));
				return systems;
			}else {
				//非超级管理员，根据授权获取可访问的系统
				List<Model> systems=sessionDao.queryUserSystems(Model.create().add("userId", userId));
				return systems;
			}
		}else {
			return new ArrayList<Model>();
		}
	}

	@Override
	public List<Model> listTree() {
		RbacSystemDao dao=factory.getDao(RbacSystemDao.class);
		List<Model> datas= dao.select(Model.create().orderBy("sysSort"));
		for (Model data : datas) {
			data.add("type", "system");
			data.add("isLeaf", false);
			data.add("isParent", true);
		}
		return datas;
	}
}
